<?php
/*******************************************************************************
 * Project: Bridge for WordPress to phpBB 3.0.x
 * File:    admin.php
 * Author:  The Greek WordPress Comunity
 * Date:    Tuesday, 1st February 2011
 * -----------------------------------------------------------------------------
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

function wpb_admin_menu()
{
    add_menu_page(
        "WP phpBB Bridge",
        "WP phpBB Bridge",
        "administrator",
        basename(__FILE__),
        "wpb_display_options",
        plugin_dir_url(__FILE__) . "/images/icon16.jpg"
    );
}

function wpb_display_options()
{
	$submit = false;
	$active = trim(get_option('wpb_active'));
	$path = trim(get_option('wpb_path'));
	$url = trim(get_option('wpb_url'));

	if(isset($_POST['action']) && ($_POST['action'] == 'wpb_update'))
	{
		$submit = true;
		$active = trim($_POST['wpb_active']);
		$path = trim($_POST['wpb_path']);
		$url = trim($_POST['wpb_url']);
	}

	if($active == "")
	{
		$active = "no";
	}

	if($path == "")
	{
		$path = dirname(dirname($_SERVER['SCRIPT_FILENAME'])) . '/phpbb3/config.php';
	}

	if($url == "")
	{
		$url = 'http://' . $_SERVER['HTTP_HOST'] . '/phpbb3/ucp.php';
	}

	if(!file_exists($path))
	{
		$active = "no";
		?>
        <div id="message" class="updated fade">
            <p>
                <?php 
                    _e('Unable to find config.php. Cannot activate bridge.', 'wp_phpbb_bridge'); 
                ?>
            </p>
        </div>
        <?php
	}

	update_option('wpb_active', $active);
	update_option('wpb_path', $path);
	update_option('wpb_url', $url);

	if($submit)
	{
		?>
        <div id="message" class="updated fade">
            <p>
                <?php 
                    _e('Options saved.', 'wp_phpbb_bridge') 
                ?>
            </p>
        </div>
        <?php
	}

?>
<div class="wrap">
	<form method="post" action="">
        <img class="icon32" src="<?php echo plugin_dir_url(__FILE__); ?>/images/icon.jpg ?>" />
		<h2>
            WP phpBB Bridge
        </h2>
		<table class="form-table">
			<tr>
				<th>
                    <label>
                        <?php 
                            _e('Path to config.php:', 'wp_phpbb_bridge'); 
                        ?>
                    </label>
                </th>
				<td>
                    <input type="text" name="wpb_path" style="width: 95%" value="<?php echo $path; ?>" />
                </td>
			</tr>
			<tr>
				<th>
                    <label>
                        <?php 
                            _e('URL to ucp.php:', 'wp_phpbb_bridge'); 
                        ?>
                    </label>
                </th>
				<td>
                    <input type="text" name="wpb_url" style="width: 95%" value="<?php echo $url; ?>" />
                </td>
			</tr>
			<tr>
				<th>
                    <label>
                        <?php 
                            _e('Activate Bridge:', 'wp_phpbb_bridge'); 
                        ?>
                    </label>
                </th>
				<td>
					<input name="wpb_active" type="radio" id="active_yes" value="yes" <?php
						if ($active == "yes")
						{
							echo 'checked="checked" ';
						}
					?>/> <?php _e('Yes', 'wp_phpbb_bridge'); ?>
					<br />
					<input name="wpb_active" type="radio" id="active_no" value="no" <?php
						if ($active != "yes")
						{
							echo 'checked="checked" ';
						}
					?>/> <?php _e('No', 'wp_phpbb_bridge'); ?>
				</td>
			</tr>
		</table>
		<p class="submit">
			<input type="hidden" name="action" value="wpb_update" />
			<input type="submit" name="Submit" value="<?php _e('Update Options', 'wp_phpbb_bridge') ?>" />
		</p>
	</form>
</div>
<?php
}
?>